const { usersModel } = require('../models/usersModel')
const { foodsModel } = require('../models/foodsModel')
const { code2Session } = require('../../configs/wxConfig')
const jwt = require('jsonwebtoken')

// 获取用户信息
module.exports.getUserInfo = async ({ _id }) => {
    const data = await usersModel.findOne({ _id })
        .populate({ path: 'collect' })
        .populate({ path: 'browse' })
        .populate({ path: 'likes' })
    return data
}

// 修改用户信息
module.exports.modifyUsers = async (params) => {
    const data = await usersModel.findByIdAndUpdate({ _id: params._id }, { ...params })
        .populate({ path: 'collect' })
        .populate({ path: 'browse' })
        .populate({ path: 'likes' })
    // console.log(data);
    return data
}

// 收藏和取消收藏
module.exports.collection = async ({ _id, foodsId }) => {
    const data1 = await foodsModel.findOne({ _id: foodsId })
    const data2 = await usersModel.findById({ _id })
    if (data2.collect.indexOf(data1._id) == -1) {
        // 收藏
        await usersModel.update({ _id }, { $push: { collect: data1._id } })
        const a = await usersModel.findById({ _id })
        return {
            code: 0,
            a
        }
    } else {
        // 取消收藏
        const extra = await usersModel.findOne({ _id })
        for (let i = 0; i < extra.collect.length; i++) {
            if (extra.collect[i] == foodsId) {
                extra.collect.splice(i, 1)
            }
        }

        await usersModel.findByIdAndUpdate({ _id }, { collect: extra.collect })
        console.log(extra.collect);
        const b = await usersModel.findById({ _id })
        return {
            code: 1,
            b
        }
    }
}

// 获取收藏
module.exports.getCollection = async ({ _id }) => {
    const data = await usersModel.findOne({ _id })
        .populate({ path: 'collect' })
    console.log(data);
    return data.collect
}

// 浏览商品
module.exports.browse = async ({ _id, foodsId }) => {
    const data1 = await foodsModel.findOne({ _id: foodsId })
    const data = await usersModel.update({ _id }, { $push: { browse: data1._id } })
    console.log(111, data);
    return data
}

// 获取浏览商品
module.exports.getBrowse = async ({ _id }) => {
    const data = await usersModel.findOne({ _id })
        .populate({ path: 'browse' })
    console.log(111, data.browse);
    return data.browse
}

// 微信授权登录
module.exports.wxLogin = async (params) => {
    try {
        // 通过code、appId、appSecret获取session_key、openid，并判断openid是否存在
        const { code, userInfo } = params
        const data = await code2Session(code)
        const { session_key, openid } = data.data
        if (!openid) return { message: 'openid不存在' }

        // 通过openid在数据库是否已保存，如果登录过就新建，然后获取当前用户在数据库中的 id 并和openid、session_key一起保存到token中返回
        const user = await usersModel.findOne({ openid })
        let userMsg = null
        if (user != null) {
            userMsg = user
        } else {
            userMsg = await usersModel.create({ openid, ...userInfo })
        }

        // 生成token并返回前端
        const token = jwt.sign({ openid, _id: userMsg._id, session_key }, 'hello')
        return { token, userMsg }
    } catch (error) {
        console.log('error', error);
    }
}